<?php

/**
 * This is application main file.
 * Use it as a bootstrap file.
 */


// application base dir
define('APP_BASE_DIR', realpath(PSA_BASE_DIR . '/../'));


// include aplication functions
include 'functions.php';


class Main extends Psa_Router{

	
	function psa_main(){
		
		// put config array to the registry
		include APP_BASE_DIR . '/config.php';
		$this->psa_registry->CFG = &$CFG;
		
		// put some default values in result object that you'll need on every request
		if(@$_SERVER["HTTP_X_REQUESTED_WITH"] == 'XMLHttpRequest')
			$this->psa_result->ajax_request = 1;
		else
			$this->psa_result->ajax_request = 0;
		
		$this->psa_result->basedir_web     = $this->psa_registry->basedir_web;
		$this->psa_result->html_page_title = $this->psa_registry->CFG['app_name'];
		$this->psa_result->app_version     = $this->psa_registry->CFG['app_ver'];
						
		// start session
		session_start();
		
		// call action method
		try{
			$this->dispach();
		}
		// redirect to login screen if Unauthorized_Exception is raised
		catch(Unauthorized_Exception $e){
			$main_view = new Main_View();
			if($this->psa_result->unsuccessful_authorize)
				$main_view->redirect('default/login/unsuccessful');
			else
				$main_view->redirect('default/login');
		}
		// show exception screen for any uncaught exception
		catch(Exception $e){
			try{
				check_login();
			}
			catch(Unauthorized_Exception $e){
				$main_view = new Main_View();
				if($this->psa_result->unsuccessful_authorize)
					$main_view->redirect('default/login/unsuccessful');
				else
					$main_view->redirect('default/login');
			}
			$main_view = new Main_View();
			$main_view->uncaught_exception($e);
		}
	}
}
